#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>


/* 1-确定主设备号                                                                 */
static int major = 0;
static struct class *led_class;
static struct gpio_desc *gpiodesc;



/* 3-实现对应的open/read/write等函数，填入file_operations结构体                   */
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}

/* write(fd, &val, 1); */
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
    int err;
    char status;
    //struct inode *inode = file_inode(file);
    //int minor = iminor(inode);
    
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    err = copy_from_user(&status, buf, 1);

    /* 4.4 根据status设置gpio */
    gpiod_set_value(gpiodesc, status);
    
    return 1;
}

static int led_drv_open (struct inode *node, struct file *file)
{
    //int minor = iminor(node);
    
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    /* 4.3 设置gpio方向 */
    gpiod_direction_output(gpiodesc, 0);
    
    return 0;
}

static int led_drv_close (struct inode *node, struct file *file)
{
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}

/* 2-定义自己的file_operations结构体                                              */
static struct file_operations led_drv = {
    .owner   = THIS_MODULE,
    .open    = led_drv_open,
    .read    = led_drv_read,
    .write   = led_drv_write,
    .release = led_drv_close,
};

/*
*******************************************************************
    platform_driver 函数
*******************************************************************
*/


/*
 * 4、从platform device获取GPIO
 *    把file_operations结构体告诉内核：注册驱动程序
 */
static int chip_demo_gpio_probe(struct platform_device *dev)
{
    int err;

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    printk("device ptr : %p\n", dev);
    
    /* 4.1 从设备树获取led-gpios属性 */
    gpiodesc = gpiod_get(&dev->dev, "led", 0);
    if(IS_ERR(gpiodesc))
    {
        dev_err(&dev->dev, "Failed to get GPIO for led !!!\n");
        return PTR_ERR(gpiodesc);
    }

    /* 4.2 注册file_operetions结构体 */
    major = register_chrdev(0, "xxhled", &led_drv); /* /dev/xxhled */
    led_class = class_create(THIS_MODULE, "xxh_led_class"); /* /sys/class/xxh_led_class */
    if(IS_ERR(led_class))
    {
        printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
        unregister_chrdev(major, "xxhled");
        class_destroy(led_class);
        gpiod_put(gpiodesc);
        return PTR_ERR(led_class);
    }
    
    device_create(led_class, NULL, MKDEV(major, 0), NULL, "xxhled%d", 0); /* /dev/100ask_led0,1,... */
    
    return 0;
}

static int chip_demo_gpio_remove(struct platform_device *dev)
{
    /* 注销顺序同probe相反 */
    #if 1
    device_destroy(led_class, MKDEV(major, 0));
    class_destroy(led_class);
    unregister_chrdev(major, "xxhled");
    gpiod_put(gpiodesc);
    #else
    /* 不相反的结果如何呢? */
    unregister_chrdev(major, "xxhled");
    class_destroy(led_class);
    gpiod_put(gpiodesc);
    device_destroy(led_class, MKDEV(major, minor));
    #endif
}


static const struct of_device_id xxh_leds[] = {
    {.compatible = "xxh,leddrv"},
    {},
};

/* 1、定义platform_drver结构体 */
static struct platform_driver chip_demo_gpio_driver = {
    .probe  = chip_demo_gpio_probe,
    .remove = chip_demo_gpio_remove,
    .driver = {
        .name   = "xxh_led",
        .of_match_table = xxh_leds,
    },
};

/* 2、入口函数：注册platform_driver结构体 */
static int __init led_init(void)
{
    int err;
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    err = platform_driver_register(&chip_demo_gpio_driver);

    return err;
}


/* 3、 有入口函数就应该有出口函数：卸载驱动程序时，就会去调用这个出口函数           */
static void __exit led_exit(void)
{
    int err;

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    platform_driver_unregister(&chip_demo_gpio_driver);

}


/* 7. 其他完善：提供设备信息，自动创建设备节点                                     */

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");

